package models

import (
	"gopkg.in/mgo.v2/bson"
	"lottery/libs/mymgo"
)

const (
	SITE_COL_STR = "site"
)

type Site struct {
	Id          int    `json:"id" bson:"_id"`                // 站点ID
	PlatformId  int    `json:"platformId" bson:"platformId"` // 平台ID
	Name        string `json:"name" bson:"name"`             // 站点名称
	SiteUrl     string `json:"siteUrl" bson:"siteUrl"`       // 站点URL
	IsDeleted   bool   `json:"isDeleted" bson:"isDeleted"`
	CreateTime  int64  `json:"createTime" bson:"createTime"`
	UpdateTime  int64  `json:"updateTime" bson:"updateTime"`
	DeletedTime int64  `json:"deletedTime" bson:"deletedTime"`
}

func SaveSite(site *Site) (err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}

	site.Id = sess.AutoIncId(SITE_COL_STR)
	return sess.Insert(SITE_COL_STR, site)
}

func GetSites() (sites []*Site, err error) {

	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}

	sites = make([]*Site, 0, 100)
	err = sess.All(SITE_COL_STR, bson.M{"isDeleted": false}, &sites)
	return
}

func UpdateSite(site *Site) (err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	return sess.Update(SITE_COL_STR, bson.M{"_id": site.Id}, &site)
}

func ExistsPlatformId(platformId int) (exist bool) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	return sess.Exist(SITE_COL_STR, bson.M{"platformId": platformId})
}
